package com.chuzhiyun.chengguoxy.feature.internship.clockin;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chuzhiyun.chengguoxy.R;
import com.chuzhiyun.chengguoxy.databinding.ActivityRetroactiveBinding;
import com.chuzhiyun.chengguoxy.feature.base.BaseActivity;
import com.chuzhiyun.chengguoxy.feature.bean.WorkTagsImageBean;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.chuzhiyun.chengguoxy.utlis.CustomClickListener;
import com.chuzhiyun.chengguoxy.utlis.NewItemClickListener;
import com.chuzhiyun.chengguoxy.utlis.NewOnItemChildClickListener;
import com.chuzhiyun.chengguoxy.utlis.PictureSelectUtil;
import com.shehuan.nicedialog.BaseNiceDialog;
import com.shehuan.nicedialog.NiceDialog;
import com.shehuan.nicedialog.ViewConvertListener;
import com.shehuan.nicedialog.ViewHolder;
import com.vondear.rxtool.RxFileTool;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RetroactiveActivity extends BaseActivity<ActivityRetroactiveBinding> {

    private String mood = "";
    private String date;
    private String reason = "";
    private List<String>deleteFile = new ArrayList<>();
    private List<WorkTagsImageBean> datas = new ArrayList<>();
    private RetroactiveActivity.MyAdapter myAdapter;

    @Override
    public void initView(View view) {
        viewBinding.navView.ivBack.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                onBackPressed();
            }
        });
        viewBinding.navView.tvTitle.setText("补卡");
        initEditLength(viewBinding.editLiyou,viewBinding.tvLengthTip,500);
        viewBinding.xinQing1.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                mood = "good";
                viewBinding.xinQingText0.setTextColor(Color.parseColor("#FEC43C"));
                viewBinding.xinQingText1.setTextColor(Color.parseColor("#2C2C2C"));
                viewBinding.xinQingText2.setTextColor(Color.parseColor("#2C2C2C"));
            }
        });
        viewBinding.xinQing2.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                mood = "moderate";
                viewBinding.xinQingText1.setTextColor(Color.parseColor("#FEC43C"));
                viewBinding.xinQingText0.setTextColor(Color.parseColor("#2C2C2C"));
                viewBinding.xinQingText2.setTextColor(Color.parseColor("#2C2C2C"));
            }
        });
        viewBinding.xinQing3.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                mood = "bad";
                viewBinding.xinQingText2.setTextColor(Color.parseColor("#FEC43C"));
                viewBinding.xinQingText0.setTextColor(Color.parseColor("#2C2C2C"));
                viewBinding.xinQingText1.setTextColor(Color.parseColor("#2C2C2C"));
            }
        });
        viewBinding.editLiyou.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                reason = viewBinding.editLiyou.getText().toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
        date = getIntent().getStringExtra("date");
        viewBinding.tvStartTime.setText(date);

        WorkTagsImageBean bean = new WorkTagsImageBean();
        bean.setTagsType("0");
        datas.add(bean);

        viewBinding.recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        myAdapter = new MyAdapter(R.layout.item_worktags_input, datas);
        viewBinding.recyclerView.setAdapter(myAdapter);
        myAdapter.addChildClickViewIds(R.id.delete_Img);
        myAdapter.notifyDataSetChanged();

        myAdapter.setOnItemClickListener(new NewItemClickListener() {
            @Override
            protected void onSingleClick(BaseQuickAdapter adapter, View view, int position) {
                WorkTagsImageBean bean = datas.get(position);
                if (bean.getTagsType().equals("0"))
                {
                    getPhoto((FragmentActivity) mContext);
                }
            }
        });

        myAdapter.setOnItemChildClickListener(new NewOnItemChildClickListener() {
            @Override
            protected void onSingleClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.delete_Img) {
                    WorkTagsImageBean bean = datas.get(position);
                    if (bean.getTagsType().equals("1"))
                    {
                        datas.remove(position);
                        deleteImageList();
                        myAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

        viewBinding.rlSubmit.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                retroactiveClick();
            }
        });
    }

    private void retroactiveClick(){

        String url = NetApi.postPunchFixes();
        HashMap<String,String>param = new HashMap<>();
        param.put("date",date);
        param.put("reason",reason);
        ArrayList<File> pictures = new ArrayList<>();
        if (datas.size() > 1) {
            for (int i = 0; i < datas.size(); i++) {
                WorkTagsImageBean imageBran = datas.get(i);
                if (imageBran.getTagsType().equals("1")) {
                    pictures.add(imageBran.getImageFile());
                }
            }
        }
        showLoadingDialog();
        NetService.getInstance().postFileWithParam(url, param, pictures, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                dismissLoadingDialog();
                if (statusCode==201){
                    onBackPressed();
                }
            }

            @Override
            public void onError(String error) {
                dismissLoadingDialog();
            }
        });
    }

    @Override
    protected ActivityRetroactiveBinding getViewBinding() {
        return ActivityRetroactiveBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    public static class MyAdapter extends BaseQuickAdapter<WorkTagsImageBean, BaseViewHolder> {
        public MyAdapter(int layoutResId, List<WorkTagsImageBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder helper, WorkTagsImageBean item) {
            if (item.getTagsType().equals("1"))
            {
                Bitmap bitmap= BitmapFactory.decodeFile(item.getImageFile().toString());
                helper.setImageBitmap(R.id.tags_Img,bitmap);
                helper.setVisible(R.id.delete_Img,true);
            }
            else
            {
                helper.setImageResource(R.id.tags_Img,R.mipmap.add_work);
                helper.setVisible(R.id.delete_Img,false);
            }
        }
    }

    private void getPhoto(FragmentActivity myActivity) {

        NiceDialog.init()
                .setLayoutId(R.layout.dialog_head_img)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        holder.setOnClickListener(R.id.select_photo_dialog_btn_camera, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                takePhotoCamera();
                            }
                        });
                        holder.setOnClickListener(R.id.select_photo_dialog_btn_album, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                takePhotoAlbum();
                            }
                        });
                        holder.setOnClickListener(R.id.select_photo_dialog_btn_cancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                    }
                })
                .setGravity(Gravity.BOTTOM)
                .show(myActivity.getSupportFragmentManager());
    }

    private void takePhotoCamera() {
        PictureSelectUtil.with(this)
                .camera()
                .setCallback(new PictureSelectUtil.OnCallback() {
                    @Override
                    public void onCallback(Uri uri) {
                        File mFile = RxFileTool.getFilePhotoFromUri((Activity) mContext,uri);
                        WorkTagsImageBean bean = new WorkTagsImageBean();
                        bean.setImageFile(mFile);
                        bean.setTagsType("1");
                        datas.add(bean);
                        changeImageList();
                        myAdapter.notifyDataSetChanged();
                    }
                }).select();
    }

    private void takePhotoAlbum(){
        PictureSelectUtil.with(this)
                .gallery()
                .setCallback(new PictureSelectUtil.OnCallback() {
                    @Override
                    public void onCallback(Uri uri) {
                        File mFile = RxFileTool.getFilePhotoFromUri((Activity) mContext,uri);
                        WorkTagsImageBean bean = new WorkTagsImageBean();
                        bean.setImageFile(mFile);
                        bean.setTagsType("1");
                        datas.add(bean);
                        changeImageList();
                        myAdapter.notifyDataSetChanged();
                    }
                }).select();
    }

    private void changeImageList() {
        WorkTagsImageBean temp = new WorkTagsImageBean();
        boolean flag = false;
        for(int i = 0; i < datas.size(); i++){
            WorkTagsImageBean student = datas.get(i);
            if (student.getTagsType().equals("0")) {
                temp = student;
                flag = true;
                datas.remove(i);
                break;
            }
        }
        if (flag) {
            if (datas.size()<4)
            {
                datas.add(temp);
            }
        }
    }

    private void deleteImageList() {
        boolean isFlag = false;
        for(int i = 0; i < datas.size(); i++){
            WorkTagsImageBean student = datas.get(i);
            if (student.getTagsType().equals("0")) {
                isFlag = true;
            }
        }
        if (!isFlag)
        {
            WorkTagsImageBean temp2 = new WorkTagsImageBean();
            temp2.setTagsType("0");
            datas.add(temp2);
        }
    }
}